const db = require('../config/db');
const Sequelize = db.sequelize;
const Article = Sequelize.import('../schema/article');
Article.sync({
    force: false
})

class ArticleModel {

    /**
     * 创建文章
     */

     static async create(params) {
        console.log(params);
        return await Article.create(params);
     }

     /**
      * 更新文章
      * 
      */
     static async update(id, params){
         return await Article.update(params, {
             where: {
                 id
             }
         })
     }

    /**
     * 获取文章列表
     */

     static async list(params) {
         let res = null;
         let page = params.page ? page: 1;
         res = await Article.findAndCountAll({
             limit: 10,
             offset: (page-1) * 10,
             order: [
                 ['id', 'DESC']
             ],
             attributes: {
                exclude: ['is_del']
             }
         })

         return {
            rows: res.rows,
            count: res.count,
            total_pages: Math.ceil(res.count / 10)
         }
     }
    /**
     * 获取文章详情数据
     * @param id 文章id
     */

    static async detail(id) {
        return await Article.findOne({
            where: {
                id,
                is_del: 0
            },
            attributes: {
                exclude: ['is_del']
            }
        })
    }
}

module.exports = ArticleModel